package com.kun.util;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.codec.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created by likun on 2017/4/18.
 */
public class ObjectUtil {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(ObjectUtil.class);
    /**
     * 将对象反序列化
     * @param str
     * @return
     */
    public static Object deserialize(String str) {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            bis = new ByteArrayInputStream(Base64.decode(str));
            ois = new ObjectInputStream(bis);
            return ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("对象反序列化异常", e);
        } finally {
            try {
                ois.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 将对象序列化
     * @param obj
     * @return
     */
    public static String serialize(Object obj) {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            return Base64.encodeToString(bos.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("对象序列化异常", e);
        } finally {
            try {
                oos.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 对象转json
     * @param object
     * @return
     */
    public static String objectTojson(Object object){
        ObjectMapper objectMapper = new  ObjectMapper();
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
            logger.error("对象转json失败",e);
        } catch (JsonMappingException e) {
            e.printStackTrace();
            logger.error("对象转json失败",e);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("对象转json失败",e);
        }
        return null;
    }

    //将json反序列化为对象
    public static Object objectFromJson(String json,Class clazz){
        ObjectMapper objectMapper = new  ObjectMapper();
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
            logger.error("json转对象失败",e);
        } catch (JsonMappingException e) {
            e.printStackTrace();
            logger.error("json转对象失败",e);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("json转对象失败",e);
        }
        return null;
    }
}
